<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController{

    private $Goods='';
    private $Cate='';
    private $Business='';
    private $SellerGoods='';
    private $Cart='';
    private $userID='';
    private $Community = '';
    private $Seller='';

    function __construct()
    {
        parent::__construct();
        $this->Goods=D('Goods');   // 实例化Goods表
        $this->Cate=D('Category');  // 实例化Category表
        $this->Business=D('Business');  // 实例化商户信息表
        $this->SellerGoods=D("Seller_goods");   // 实例化商户商品表
        $this->Cart=D('Cart');
        $this->userID=session("userID");
        $this->Community=D("Community");
        $this->Seller=D('Seller');
    }

    /*
     *
     * */
    function index()
    {

        echo "---1-1--1-1-1-1---"; exit;
        $neverSellerID=session("seller_id");     // 附近门店的ID

        $yanG1['seller_id']="0";
        $goods = $this->Goods->where($yanG1)->select();   // 导出所有的商品信息  seller_id=0 表示自营
        $cate  = $this->Cate->field(array('id','name','order'))->order('order desc')->select();    // 导出所有的栏目信息
        //此用户 购物车中的商品
        $cateUserNum=$this->Cart->query("select ddgc_goods.id,ddgc_cart.goods_id,ddgc_cart.number from ddgc_goods INNER JOIN ddgc_cart ON ddgc_goods.id=ddgc_cart.goods_id AND ddgc_cart.user_id={$this->userID}");

        foreach($goods as $ke=>$ro)
        {
            foreach($cateUserNum as $re=>$vo)
            {
                if($ro['id']==$vo['id'])
                {
                    if($vo['number']>'0'){
                        $goods[$ke]['cart_number']=$vo['number'];     //购物车中的商品数量  添加到 $goods数组中
                    }else{
                        $goods[$ke]['cart_number']="";     //购物车中的商品数量  添加到 $goods数组中
                    }
                }
            }
        }

        // 购物车中的商品处理 END

        $businessFull = $this->Seller->field(array('id','nick'))->select();   //  导出所有的商户信息

        if($neverSellerID!="")  // 通过 neverSellerID 找到 对应的信息
        {
            $business = $this->Seller->field(array('id','nick'))->where("id=".$neverSellerID)->find();
            $this->assign("business",$business);
        }

        foreach($cate as $key=>$r)
        {
            $sumNum="";
            foreach($goods as $rey=>$v)
            {
                if($r['id']==$v['cate_id'])
                {
                    $arr[$key][]=$v;
                    if($v['cart_number']!="")
                    {
                        $sumNum=$sumNum+$v['cart_number'];
                    }
                }
            }
            $cate[$key]['sumCartNum']=$sumNum;
        }

        if($_POST)
        {
            $brr=$this->Goods->where("seller_id=".$_POST['sellerID'])->select();    // $_POST['sellerName'] 中保存着 ID   仅检索出商户的商品

            foreach($brr as $keyy=>$roo) {
                foreach ($cateUserNum as $reyy => $voo)
                {
                    $brr[$keyy]['sellerCateUserNum']=$voo['number'];
                }
            }
            $crr=$this->Seller->where("id=".$_POST['sellerID'])->find();

            $this->assign("sellerProduct",$brr);
            $this->assign("sellerName",$crr['nick']);
        }else{
            if($business!="")
            {
                $brr=$this->Goods->where("seller_id=".$business['id'])->select();
                foreach($brr as $keyy=>$roo) {
                    foreach ($cateUserNum as $reyy => $voo)
                    {
                        $brr[$keyy]['sellerCateUserNum']=$voo['number'];
                    }
                }
                $crr=$this->Seller->where("id=".$business['id'])->find();
            }else{
                $brr=$this->Goods->where("seller_id!=0")->limit(1)->select();

                if(!empty($brr))
                {
                    $crr=$this->Seller->where("id=".$brr[0]['seller_id'])->find();
                }
            }

            $this->assign("sellerProduct",$brr);
            $this->assign("sellerName",$crr['nick']);
        }

        $cartNum=$this->Cart->where("user_id=".$this->userID)->sum('number');
        $this->assign("cartNum",$cartNum);

        $cartNumPrice=$this->Cart->where("user_id=".$this->userID)->select();

        $cartPrice='';
        foreach($cartNumPrice as $key=>$r)
        {
            $cartPrice += $r['shop_price']*$r['number'];
        }

        $this->assign("cartPrice",$cartPrice);
        $this->assign("product",$arr);
        $this->assign("cate",$cate);

        $this->assign("businessFull",$businessFull);
        $this->display("index");
    }

    function location()
    {
     	$lat = $_GET['lat'];
	    $lng = $_GET['lng'];
	
        $square=A('Location');
        $squares = $square->returnSquarePoint($lng, $lat);

        $sellerID = $this->Community->where("x<>0 and x>{$squares['right-bottom']['lat']} and X<{$squares['left-top']['lat']} and Y>{$squares['left-top']['lng']} and Y< {$squares['right-bottom']['lng']}")->find();
//        echo $sellerID["seller_id"];

        session("seller_id",$sellerID['seller_id']);
//        session("seller_id","123456");
        $this->Index($sellerID['seller_id']);
    }

    /*
     * 通过ajax传过来 商品的ID
     * $_POST['productID'] 为商品的ID
     * */
    function addCart()
    {
        $data['id']='';
        $data['user_id']=$this->userID;    //临时设置一个userID
        $data['goods_id']=$_POST['productID'];    // 商品的DI

        /*通过productID找到对应商品的信息*/
        $arr=$this->Goods->where("id=".$data['goods_id'])->find();   // 取得商品
        $data['goods_sn']=$arr['goods_sn'];
        $data['name']=$arr['name'];
        $data['market_price']=$arr['market_price'];
        $data['shop_price']=$arr['shop_price'];

        if($_POST['name']>$arr['number'])
        {
            echo "库存数量不足！！！"; exit;
        }else{
            $data['number']=$_POST['number'];    //  加入购物车的数量
        }

        /*
         * 将数据存入购物车
         * */

        //1.从购物车表中找出goods_id 的商品

        $cond['goods_id']=$data['goods_id'];
        $cond['user_id']=$data['user_id'];
        $brr=$this->Cart->where($cond)->find();
        if(empty($brr))      // 判断一下购物车中是否已经存在这个商品
        {
            // 购物车中没有goods_id 这个商品 ,  第一次往购物车中添加
            $yanG1=$this->Cart->data($data)->add();
//            if($yanG1==true)
//            {
//                echo "成功加入购物车!!!"; exit;
//            }else{
//                echo "加入购物车失败-1!!!"; exit;
//            }
        }else{
            //  购物车中已经有goods_id 这个商品, 只需要改变 number 这个字段的值
            $save['number']=$data['number'];
            $yanG2=$this->Cart->where("id=".$brr['id'])->data($save)->save();
//            if($yanG2==true)
//            {
//                echo "加入购物车成功!!!"; exit;
//            }else{
//                echo "加入购物车失败-2!!!"; exit;
//            }
        }

        $cart['Num']=$this->Cart->where("user_id=".$data['user_id'])->sum('number');

        //计算购物车中的商品总价

        $yanGone=$this->Cart->where("user_id=".$data['user_id'])->select();

        $price='';

        foreach($yanGone as $key=>$ro)
        {
            $price+=$ro['number']*$ro['shop_price'];
        }
        $cart['price']=$price;
        $cart1=$cart['Num']."/".$cart['price'];
        echo $cart1;
        exit;
    }

    function lbs()
    {
        $this->display("lbs");
    }
}